#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"

#define NOISY_TEST 0

void testSalvager(){
	int player, preHandCount, handCount;
	int preNumBuys, numBuys;
	int prePlayedCount, playedCount;
	int seed = 50;
	int numPlayer = 3;
	struct gameState state;
	int cur, it;
	int k[10] = {adventurer, council_room, feast, gardens, mine,
					remodel, smithy, village, baron, great_hall};


	// set choices to playDom example
	int choice1 = 0, choice2 = 0, choice3 = 0, handPos = 0, bonus=0;

	for(player = 0; player < numPlayer; player++){

		// test 2 possible states with choice1
		for(choice1 = 0; choice1 < 2; choice1++){
			// initialize game
	   		int r = initializeGame(numPlayer, k, seed, &state);
			if(r < 0){
	    		printf("Error initializing game...\n");
			}

			// set the player
			state.whoseTurn = player;
			// compensate for initializeGame where other players don't draw their cards
			if(player > 0){
				for(it = 0; it < 5; it++){
                 	drawCard(state.whoseTurn, &state);
				}
			}

			// verify empty hand
			if(state.handCount[player] == 0){
	         	state.handCount[player] = 1;
	#if (NOISY_TEST == 1)
				printf("Player %d handcount was empty, new handcount: %d\n", player, state.handCount[player]);
	#endif
			}

			// force salvager card to be the first card in hand
			state.hand[player][0] = salvager;		

			// Determine preconditions
			preHandCount = state.handCount[player];
			preNumBuys = state.numBuys;
			prePlayedCount = state.playedCardCount; // discard pile
            int preCoins = state.coins; // check coins for when choice1==1

			// sanity - verify discard card will go to playedCard pile.
			int cardDiscard1 = state.hand[player][handPos];

			// check getCost here
			int getCostChoice1 = getCost(handCard(choice1, &state));
	#if (NOISY_TEST == 1)
			printf("What is result of getCost? %d\n", getCostChoice1);
	#endif

			cur = cardEffect(salvager, choice1, choice2, choice3, &state, handPos, &bonus);
	        if(cur != 0){
	        	printf("ERROR: Salvager card could not be used.\n");
				break;
			}                                                        
			// obtain postconditions
			handCount = state.handCount[player];
			numBuys = state.numBuys;
			playedCount = state.playedCardCount;
			int cardDiscardCheck1 = state.playedCards[playedCount-1];

			
			if(choice1){
            	// 2 cards are discarded since 1 is trashed
    #if (NOISY_TEST == 1)
            	printf("Player %d Hand Count - Before: %d. Expected: %d. Actual: %d\n", player, preHandCount, preHandCount - 2, handCount);
	#endif
				assert(preHandCount - 2 == handCount);
				int postCoins = state.coins;
	#if (NOISY_TEST == 1)
				printf("**Player %d Coins Check - Before: %d. Expected: %d. Actual: %d\n", player, preCoins, preCoins + getCostChoice1, postCoins);
	#endif
				assert(preCoins + getCostChoice1 == postCoins);
			}else{
				// 1 card is discarded
	#if (NOISY_TEST == 1)
            	printf("Player %d Hand Count - Before: %d. Expected: %d. Actual: %d\n", player, preHandCount, preHandCount - 1, handCount);
	#endif
				assert(preHandCount - 1 == handCount);
			}
	#if (NOISY_TEST == 1)
			printf("Player %d Number of Buys - Before: %d. Expected: %d. Actual: %d\n", player, preNumBuys, preNumBuys + 1, numBuys);
	#endif
			assert(preNumBuys + 1 == numBuys);
	#if (NOISY_TEST == 1)
			printf("Expected: %d. Actual: %d\n", cardDiscard1, cardDiscardCheck1);
	#endif
			assert(cardDiscard1 == cardDiscardCheck1);
	#if (NOISY_TEST == 1)
			printf("Player %d Played count - Before: %d. After: %d\n", player, prePlayedCount, playedCount);
	#endif
			assert(prePlayedCount + 1 == playedCount);
		}
		
	}
	printf("Finishing testSalvager()\n");
}

int main(){
	testSalvager();
	return 0;
}
